home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
- *
- * This file is part of clicknlearn.
- *
- * clicknlearn is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * clicknlearn is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with clicknlearn. If not, see <http://www.gnu.org/licenses/>.
- */
-
- Components.utils.import("resource://clicknlearn/ext/Observers.js");
- Components.utils.import("resource://clicknlearn/cnlprefs.js");
-
- function DicLookup(){
- this.currentWord = "";
- this.currentRemind = "";
- this.remember = false;//has remember the current word
- this.div = null;
- this.divId = "clicknlearnDiv";
- this.request = Object;
-
- this._initDicServices();
- Observers.add("cnl_dicservicesPreferencesChanged", this._initDicServices, this);
-
- this._activeDicServiceIndex = this._defaultDicServiceIndex = 0;
- }
-
- DicLookup.prototype._initDicServices = function() {
- var dicts = CNL_Prefs.getDicsArrayPref();
- if(dicts.length == 0)
- dicts =
- [['true', 'tratu.vn', 'http://tratu.vn/dispatchaddon.php?dict=en_vn&title=',
- '<i>Bạn đang tra từ', '']
- ,['true', 'vdict.com', 'http://vdict.com/fsearch.php?dictionaries=eng2vie_vie2eng_foldoc&word=',
- ' <!-- Database --><!-- Database --><!-- Database -->Not Found.', '']
- ,['true', 'edictx.com', 'http://edictx.com/edictx/?dict=1&mode=addon&word=',
- ' was not found!<', '']
- ,['true', 'tudientiengviet.net', 'http://www.tudientiengviet.net/get.php?mode=quickdict&dict=en-vi&word=',
- 'Các từ tương tự: <br><ul>', '']];
- this.dicServices = [];
- for(var i in dicts)
- if(dicts[i][0] == "true")
- this.dicServices.push(new DicService(dicts[i][1], dicts[i][2], dicts[i][3], dicts[i][4]));
- }
-
- DicLookup.prototype.formatCurrentWord = function() {
- this.currentWord = CNLUtils.formatWord(this.currentWord);
- }
-
- DicLookup.prototype.reset = function() {
- this.clearRequest();
- CNL.removeDiv(this.div);
- this.div = null;
- }
-
- DicLookup.prototype.clearRequest = function() {
- if (this.request != null)
- try {
- this.request.abort();
- } catch (e) {
- }
- this.request = null;
- }
-
- DicLookup.prototype.printLookingUp = function() {
- this.div.innerHTML = CNL.stringsBundle.getFormattedString('lookingup'
- , [this.currentWord, this.dicServices[this._activeDicServiceIndex].name]);
- }
-
- DicLookup.prototype.showDivWithEmptyWord = function() {
- this.div.innerHTML = CNLUtils.replaceAntStyleString(
- "<label for='cnlwordbox'>${lookup} </label>" +
- "<input type='text' id='cnlwordbox' maxlength='60' size='15' value='' />");
- var textbox = this.div.firstChild.nextSibling;
- textbox.focus();
- }
-
- DicLookup.prototype.requestWord = function() {
- try {
- this.request = new XMLHttpRequest();
- if (this.request.readyState != 0) {
- return;
- }
- }
- catch (e) {
- return;
- }
- this._activeDicServiceIndex = this._defaultDicServiceIndex;
- this.doRequestWord(false);
- }
-
- DicLookup.prototype.doRequestWord = function(reRequestOnNotFound){
- try {
- this.request.open("GET", this.dicServices[this._activeDicServiceIndex].getUrl(this.currentWord), true);
- this.request.send(null);
- }
- catch (e) {
- return;
- }
-
- this.request.onreadystatechange = function() {
- // we have to use "CNL.DICLOOKUP.request.readyState" instead of "this.readyState"
- if(! CNL.DICLOOKUP.div)
- return;
- if (CNL.DICLOOKUP.request.readyState == 4)
- if (CNL.DICLOOKUP.request.status == 200){
- if (CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].wordNotFound(CNL.DICLOOKUP.request.responseText)){
- if(reRequestOnNotFound)
- CNL.DICLOOKUP.requestWordFromOtherSources();
- else
- CNL.DICLOOKUP.div.innerHTML = CNL.stringsBundle.getFormattedString('wordnotfoundin',
- [CNL.DICLOOKUP.currentWord, CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].name])
- + " <button id='cnl_settingsButton'><img src='chrome://clicknlearn/skin/settings24.png' /></button>"
- +"<br /><button class='clicknlearnButton' id='requestWordFromOtherSourcesButton'>"
- + CNL.stringsBundle.getString('requestWordFromOtherSources') + '</button>';
- }
- else
- CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].parseAndPrint(CNL.DICLOOKUP.request.responseText);
- }
- // else
- // if (CNL.DICLOOKUP.request.status == 404)
- // CNL.DICLOOKUP.replaceLookingUp();
- }
- }
-
- DicLookup.prototype.requestWordFromOtherSources = function(){
- this._activeDicServiceIndex ++;
- if(this._activeDicServiceIndex >= this.dicServices.length)
- this._activeDicServiceIndex = 0;
- if (this._activeDicServiceIndex == this._defaultDicServiceIndex){
- //Not found in all dic services.
- this.div.innerHTML = CNL.stringsBundle.getFormattedString('wordnotfound', [this.currentWord]);
- return;
- }
- this.printLookingUp();
- this.doRequestWord(true);
- }
-
- DicLookup.prototype.clearAll = function() {
- CNL.removeDiv(this.div);
- this.div = null;
- this.currentWord = null;
- }
-